﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using MagicApp.Models;

namespace MagicApp.Controllers
{
    public class CardsController : Controller
    {
        private CardDbContext db = new CardDbContext();
        
        // GET: Cards
        public ActionResult Index(string nameString, string typeString)
        {
            var Cards = from m in db.Cards
                        select m;

            if (Session["runCheck"] == null || nameString != null || typeString != null || !((String)Session["nameString"] == null) || !((String)Session["typeString"] == null)) //Prevents rewriting of values and function calls after the initial passthru.
            {

                if (nameString != null || typeString != null)
                {
                    Session["nameString"] = nameString;
                    Session["typeString"] = typeString;
                }

                if(!String.IsNullOrEmpty(nameString) || !((String)Session["nameString"] == null))
                {
                    

                    string entry = Session["nameString"].ToString();
                    Cards = Cards.Where(s => s.name.Contains(entry));
                }

                if (!String.IsNullOrEmpty(typeString) || !((String)Session["typeString"] == null))
                {                   
                    string entry = Session["typeString"].ToString();
                    Cards = Cards.Where(s => s.type.Contains(entry));
                }

                if (Session["runCheck"] == null || nameString != null || typeString != null)
                {
                    Session["startRange"] = 0;
                    Session["rangeMax"] = Cards.ToList().Count - 1;
                    Session["runCheck"] = 1;
                }
            }
           
            if((int)Session["startRange"] + 20 > (int)Session["rangeMax"])
            {
                try
                {
                    return View(Cards.ToList().GetRange((int)Session["startRange"], ((int)Session["rangeMax"] - (int)Session["startRange"])));
                }
                catch
                {
                    return View("badSearch");
                }
                
            }
            else 
            {
                return View(Cards.ToList().GetRange((int)Session["startRange"], 20));
            }
            
        }

        // GET: Cards/Next
        public ActionResult Next()
        {
            if (((int)Session["startRange"]) + 20 > (int)Session["rangeMax"])
            {
                return RedirectToAction("Index");
            }
            Session["startRange"] = (int)Session["startRange"] + 20;


            return RedirectToAction("Index"); ;
        }

        // GET: Cards/Previous
        public ActionResult Previous()
        {

            if (((int)Session["startRange"] - 20) < 0)
            {
                return RedirectToAction("Index"); ;
            }
            Session["startRange"] = (int)Session["startRange"] - 20;


            return RedirectToAction("Index"); ;
        }

        public ActionResult First()
        {
            Session["startRange"] = 0;
            return RedirectToAction("Index"); ;
        }
        
        public ActionResult Last()
        {
            Session["startRange"] = ((int)Session["rangeMax"] / 20) * 20;
            return RedirectToAction("Index"); ;
        }
        // GET: Cards/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Card card = db.Cards.Find(id);
            if (card == null)
            {
                return HttpNotFound();
            }
            return View(card);
        }

        // GET: Cards/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Cards/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "multiverseID,name,manaCost,convManaCost,colors,type,supertype,types,subtypes,rarity,text,flavor,artist,number,power,toughness,layout,imageName,id")] Card card)
        {
            if (ModelState.IsValid)
            {
                db.Cards.Add(card);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(card);
        }

        // GET: Cards/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Card card = db.Cards.Find(id);
            if (card == null)
            {
                return HttpNotFound();
            }
            return View(card);
        }

        // POST: Cards/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "multiverseID,name,manaCost,convManaCost,colors,type,supertype,types,subtypes,rarity,text,flavor,artist,number,power,toughness,layout,imageName,id")] Card card)
        {
            if (ModelState.IsValid)
            {
                db.Entry(card).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(card);
        }

        // GET: Cards/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Card card = db.Cards.Find(id);
            if (card == null)
            {
                return HttpNotFound();
            }
            return View(card);
        }

        // POST: Cards/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Card card = db.Cards.Find(id);
            db.Cards.Remove(card);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
